package org.phnq.stack;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * <<Class summary>>
 *
 * @author Patrick Gostovic &lt;&gt;
 * @version $Rev$
 */
public class PhnqRequestContext
{
	private static final ThreadLocal<PhnqRequestContext> currentCtx = new ThreadLocal<PhnqRequestContext>();
	
	public static PhnqRequestContext getCurrent()
	{
		return currentCtx.get();
	}
	
	public static void initForCurrentThread(String appBasePath, HttpServletRequest req, HttpServletResponse res)
	{
		currentCtx.set(new PhnqRequestContext(appBasePath, req, res));
	}
	
	public static void disposeForCurrentThread()
	{
		currentCtx.set(null);
	}
	
	private String appBasePath;
	private HttpServletRequest req;
	private HttpServletResponse res;
	
	public PhnqRequestContext(String appBasePath, HttpServletRequest req, HttpServletResponse res)
	{
		this.appBasePath = appBasePath;
		this.req = req;
		this.res = res;
	}
	
	public HttpServletRequest getRequest()
	{
		return this.req;
	}
	
	public String getRealPath(String path)
	{
		return this.appBasePath + path.replace('/', File.separatorChar);
	}
}
